package com.zmn.plat.admin.controller.product.businessline;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.admin.controller.product.productgroup.ProductGroupListController;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.product.businessline.ProductBusinessLine;

/**
 * 产品业务线基础修改Controller
 *
 * @author tanbiao
 * @since 2020/10/27 14:52
 */
@RestController
@RequestMapping("/product/businessline")
public class ProductBusinessLineModifyController extends ProductBusinessLineBaseController {
    private Logger logger = LoggerFactory.getLogger(ProductGroupListController.class);

    /**
     * 保存
     */
    @RequestMapping("/addEdit")
    @RequiresPermissions("/product/businessline/addEdit.action")
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ProductBusinessLine productBusinessLine) {

        try {
            productBusinessLineBService.addEdit(productBusinessLine, getMcStaffRealName());
            return ResponseDTO.success(true, "操作成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("新增/修改失败, Exception{}, msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }

    }

    /**
     * 描述:修改状态
     *
     * @param ids
     *            ID组成的字符串 eg: productId1,productId2
     * @param status
     *            修改的状态
     * @return: ResultDTO 执行结果
     * @auther: tanbiao
     * @since: 2020/10/27 14:52
     */
    @RequestMapping(value = "updateStatus")
    @RequiresPermissions("/product/businessline/updateStatus.action")
    @ResponseBody
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "ids", defaultValue = "") String ids,
        @RequestParam(value = "status", defaultValue = "0") int status) {

        try {
            productBusinessLineBService.updateStatus(ids, status, getMcStaffRealName());
            return ResponseDTO.success(true, "操作成功！");
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("新增/修改失败, Exception{}, msg{}", e, e.getMessage());
            return ResponseDTO.fail("操作失败！");
        }

    }

    /**
     * 功能描述 :修改排序
     *
     * @param request
     * @param response
     * @param groupId
     * @param sort
     * @return com.zmn.common.dto.ResultDTO
     * @author tanbiao
     * @modifier
     * @since 2020/10/27 14:52
     */
    @RequestMapping(value = "updateSort")
    @ResponseBody
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", defaultValue = "0") Integer groupId,
        @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (groupId == 0) {
            return ResponseDTO.fail("请选择要更改排序的产品组！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置产品组排序值！");
        }

        productBusinessLineService.updateSortByKey(groupId, sort, getMcStaffRealName());
        return ResponseDTO.success(true, "操作成功！");
    }

    /**
     * 删除
     *
     * @param relateId
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    @RequiresPermissions("product/businessline/delete")
    public ResponseDTO delete(@RequestParam(value = "relateId", defaultValue = "0") Integer relateId) {
        if (NumberUtil.isNullOrZero(relateId)) {
            return ResponseDTO.fail("请选择要删除的数据！");
        }
        productBusinessLineService.deleteByKey(relateId);
        return ResponseDTO.success(true,"删除成功！");
    }

}
